package com.astrofizzbizz.stoneedgegui.utilities;

import java.util.StringTokenizer;

public class TelescopeInfo 
{
	private double longitudeDeg;
	private double latitudeDeg;
	private double altLimit = 0;
	private double lowerDecLimit = -90;
	private double upperDecLimit = +90;
	private double lowerEastRaLimit = -90;
	private double upperWestRaLimit = 90;
	private double raDeg;
	private double decDeg;
	private double altDeg;
	private double azDeg;
	private String localSideralTime = "00:00:00.0";
	StoneEdgeIIITunnel stoneEdgeIIITunnel;
	public TelescopeInfo(StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException
	{
		this.stoneEdgeIIITunnel = stoneEdgeIIITunnel;
		update();
		
	}
	private void updatetelescopeLimits() throws StoneEdgeIIIException
	{
		StringTokenizer st = null;	
	    String info = stoneEdgeIIITunnel.executeCommand("tx tel_status", 1024)[0];
	    
	    int indexOfDone = info.indexOf("done");
	    if (indexOfDone < 0) throw new StoneEdgeIIIException("tx tel_status not responding");
		
		st = new StringTokenizer(info.substring(info.indexOf("long") + 5));
		longitudeDeg = Double.parseDouble(st.nextToken());
		
		st = new StringTokenizer(info.substring(info.indexOf("lat") + 4));
		latitudeDeg = Double.parseDouble(st.nextToken());

		st = new StringTokenizer(info.substring(info.indexOf("alt") + 4));
		altLimit = Double.parseDouble(st.nextToken());
		
		st = new StringTokenizer(info.substring(info.indexOf("south") + 6));
		lowerDecLimit = Double.parseDouble(st.nextToken());

		st = new StringTokenizer(info.substring(info.indexOf("north") + 6));
		upperDecLimit = Double.parseDouble(st.nextToken());

		st = new StringTokenizer(info.substring(info.indexOf("east") + 5));
		lowerEastRaLimit = Double.parseDouble(st.nextToken());

		st = new StringTokenizer(info.substring(info.indexOf("west") + 5));
		upperWestRaLimit = Double.parseDouble(st.nextToken());
		
	}
	private void whereIsTelescope() throws StoneEdgeIIIException
	{
		String[] whereInfo = stoneEdgeIIITunnel.executeCommand("tx where", 1024);
	    int indexOfDone = whereInfo[0].indexOf("done");
	    if (indexOfDone < 0) throw new StoneEdgeIIIException("tx where not responding");

		StringTokenizer st = new StringTokenizer(whereInfo[0]);
		String errorCheck = st.nextToken();
		if (errorCheck.toUpperCase().equals("ERROR"))
		{
			throw new StoneEdgeIIIException("Cannot find where is telescope"); 
		}
		st.nextToken();
		String raToken = st.nextToken();
		String decToken = st.nextToken();
		st.nextToken();
		st.nextToken();
		st.nextToken();
		String altToken = st.nextToken();
		String azToken = st.nextToken();
		raDeg = StarCoordUtilities.raDeg( raToken.substring(3));
		decDeg = StarCoordUtilities.decDeg(decToken.substring(4));
		altDeg = new Double(altToken.substring(4)).doubleValue();
		azDeg = new Double(azToken.substring(3)).doubleValue();
		whereInfo = stoneEdgeIIITunnel.executeCommand("lst", 1024);
		localSideralTime = whereInfo[0].substring(0, whereInfo[0].indexOf("\n"));
	}
	public void update() throws StoneEdgeIIIException
	{
		whereIsTelescope();
		updatetelescopeLimits();
	}
	public double getLongitudeDeg() {return longitudeDeg;}
	public double getLatitudeDeg() {return latitudeDeg;}
	public double getAltLimit() {return altLimit;}
	public double getLowerDecLimit() {return lowerDecLimit;}
	public double getUpperDecLimit() {return upperDecLimit;}
	public double getLowerEastRaLimit() {return lowerEastRaLimit;}
	public double getUpperWestRaLimit() {return upperWestRaLimit;}
	public double getRaDeg() {return raDeg;}
	public double getDecDeg() {return decDeg;}
	public double getAltDeg() {return altDeg;}
	public double getAzDeg() {return azDeg;}
	public String getLocalSideralTime() {return localSideralTime;}
	public static void main(String[] args) throws StoneEdgeIIIException 
	{
	    StoneEdgeIIITunnel stoneEdgeIIITunnel = new StoneEdgeIIITunnel(StoneEdgeIIITunnel.privateKey());	
	    TelescopeInfo telescopeInfo = new TelescopeInfo(stoneEdgeIIITunnel);
	    
	    System.out.println("longitudeDeg     = " + telescopeInfo.getLongitudeDeg());
	    System.out.println("latitudeDeg      = " + telescopeInfo.getLatitudeDeg());
	    System.out.println("altLimit         = " + telescopeInfo.getAltLimit());
	    System.out.println("lowerDecLimit    = " + telescopeInfo.getLowerDecLimit());
	    System.out.println("upperDecLimit    = " + telescopeInfo.getUpperDecLimit());
	    System.out.println("lowerEastRaLimit = " + telescopeInfo.getLowerEastRaLimit());
	    System.out.println("upperWestRaLimit = " + telescopeInfo.getUpperWestRaLimit());
	    System.out.println("raDeg            = " + telescopeInfo.getRaDeg());
	    System.out.println("decDeg           = " + telescopeInfo.getDecDeg());
	    System.out.println("altDeg           = " + telescopeInfo.getAltDeg());
	    System.out.println("azDeg            = " + telescopeInfo.getAzDeg());
	    System.out.println("localSideralTime = " + telescopeInfo.getLocalSideralTime());
	    
	    stoneEdgeIIITunnel.disconnect();
	    System.exit(0);
	}

}
